﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using Meqs.Logic.Core;
using Meqs.Logic.Interface;

namespace Meqs.Logic.Implement.MsSql
{
    public class DbManagerLogic : BaseSqlMapDao, IDbManagerLogic
    {
        /// <summary>
        /// 创建各学校用户并授权
        /// </summary>
        /// <param name="schoolCode"></param>
        /// <param name="folderPath"></param>
        /// <returns></returns>
        public bool CreateDbUser(string schoolCode, string folderPath)
        {
            folderPath = folderPath.TrimEnd('/') + "/MsSql";
            return Generator(schoolCode, folderPath);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="schoolCode"></param>
        /// <param name="folderPath"></param>
        /// <returns></returns>
        private static bool Generator(string schoolCode, string folderPath)
        {
            IDBAccess dbAccess = DBAccessFactory.CreateInstance(DBType.MsSQL);
            try
            {
                dbAccess.Init(ConnectionString);
                dbAccess.Open();

                DirectoryInfo di = new DirectoryInfo(folderPath);

                IList<string> list = new List<string>();
                list.Add(string.Format("CREATE SCHEMA [{0}] AUTHORIZATION [dbo]", schoolCode));

                foreach (string sql in list)
                {
                    dbAccess.RunNoQuery(sql);
                }

                foreach (FileInfo info in di.GetFiles("*school*.sql"))
                {
                    try
                    {
                        StreamReader sr = new StreamReader(info.FullName);
                        string fileContent = sr.ReadToEnd();
                        sr.Close();
                        string[] sqlArr = fileContent.Split(';');
                        foreach (string sql in sqlArr)
                        {
                            if (sql.Trim() == "") continue;
                            string s = string.Format(sql, schoolCode);
                            dbAccess.RunNoQuery(s);
                        }
                    }
                    catch (Exception ex)
                    {
                        continue;
                    }
                }
                folderPath = folderPath.TrimEnd('\\');
                StreamReader sr1 = new StreamReader(folderPath + "\\score.init.sql", Encoding.UTF8);
                string fileContent1 = sr1.ReadToEnd();
                sr1.Close();
                string[] sqlArr1 = fileContent1.Split(';');
                foreach (string sql in sqlArr1)
                {
                    if (sql.Trim() == "") continue;
                    string s = string.Format(sql, schoolCode);
                    dbAccess.RunNoQuery(s);
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
            finally
            {
                dbAccess.Close();
            }
        }
    }
}
